package com.hxgis.web.controller;


import com.es.common.domain.EsCondition;
import com.hxgis.common.base.ResultVO;
import com.hxgis.common.base.StationCache;
import com.hxgis.common.entity.Station;
import com.hxgis.common.repository.StationInfoRepository;
import com.hxgis.common.service.*;
import com.hxgis.common.utils.RectField;
import com.hxgis.common.utils.ResultVOUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/process")
@Slf4j
@Api(tags = "RainstormInfoController")
public class RainstormInfoController {

    @Autowired
    RainstormInfoService rainstormInfoService;

    @Autowired
    StationInfoRepository stationInfoRepository;

    @Autowired
    RainstormCountService rainstomCountService;

    @Autowired
    RainstormCourseService rainstormCourseService;

    @Autowired
    RainstormFeatureService featureService;


    @Autowired
    private StationExtremeService stationExtremeService;

    @Autowired
    RainstormFeatureService rainstormFeatureService;





    @ApiOperation(value="补全数据", notes="courseArea：过程编号 startTime：开始时间，endTime：结束时间，这些数据都是之前的列表接口返回的 flag:1 持续性暴雨行标 2：持续性暴雨")
    @GetMapping("/queryAnalysisVO")
    public ResultVO queryAnalysisVO(@RequestParam("courseArea") String courseArea,
                                    @RequestParam("startTime") String startTime,
                                    @RequestParam("endTime") String endTime,
                                    @RequestParam("flag") Integer flag){
        return rainstormInfoService.queryAnalysisVO(courseArea,startTime,endTime,flag);
    }

    /**
     * 根据开始时间结束时间类型分析数据
     * @param startTime
     * @param endTime
     * @return
     */
    @ApiOperation(value="第三种算法指标", notes="startTime：开始时间，endTime：结束时间")
    @GetMapping("/analysisCopyByTarget")
    public ResultVO copyAnalysisByTarget(@RequestParam("startTime") String startTime,
                                     @RequestParam("endTime") String endTime){
        return rainstormInfoService.copyAnalysisByTarget(startTime,endTime);
    }

    @ApiOperation(value="第三种方法动画", notes="courseArea：过程编号 ，startTime：开始时间，endTime：结束时间，这些数据都是之前的列表接口返回的")
    @ApiImplicitParams({
            @ApiImplicitParam(name="courseArea",value="过程编号",dataType="string", paramType = "query",example=""),
            @ApiImplicitParam(name="startTime",value="结束时间",dataType="string", paramType = "query",example="20190723"),
            @ApiImplicitParam(name="endTime",value="开始时间",dataType="String", paramType = "query",example = "20190712")})
    @GetMapping("/getCopyCourseAnimation")
    public ResultVO getCopyCourseAnimation(@RequestParam("courseArea") String courseArea,@RequestParam("startTime") String startTime,@RequestParam("endTime") String endTime){
        return rainstormInfoService.getCopyCourseAnimation(courseArea,startTime,endTime);
    }



    @ApiOperation(value="全国持续性暴雨特征指标按照年分组频次", notes="startTime：开始时间，endTime：结束时间 flag: total south north middle")
    @GetMapping("/feature/area/byFrequency")
    public ResultVO rainAreaFeatureTarget(@RequestParam("startTime") String startTime,
                                          @RequestParam("endTime") String endTime,
                                          @RequestParam("flag") String flag){
        return ResultVOUtil.success(featureService.featureAreaFrequencyTarget(startTime, endTime ,flag));
    }

    @ApiOperation(value="全国持续性暴雨特征指标持续时间和相当暴雨日接口", notes="startTime：开始时间，endTime：结束时间,continueDays: 3 4 5 对象三个图，flag:1：持续天数的图 2.相当暴雨日")
    @GetMapping("/feature/continue/target")
    public ResultVO featureContinueDaysTarget(@RequestParam("startTime") String startTime,
                                            @RequestParam("endTime") String endTime,
                                              @RequestParam("continueDays") Integer continueDays,
                                              @RequestParam("flag") Integer flag){
        return ResultVOUtil.success(featureService.featureContinueDaysTarget(startTime, endTime, continueDays, flag));
    }

    @ApiOperation(value="全国持续性暴雨特征指标", notes="startTime：开始时间，endTime：结束时间")
    @GetMapping("/feature/target")
    public ResultVO rainFeatureTarget(@RequestParam("startTime") String startTime,
                                      @RequestParam("endTime") String endTime){
        return ResultVOUtil.success(featureService.rainFeatureTarget(startTime, endTime));
    }

    @ApiOperation(value="暴雨组动画接口暂时无效", notes="groupAreaFlag：组过程标识")
    @GetMapping("/group/animation/target")
    public ResultVO groupAnimationTarget(@RequestParam("groupAreaFlag") String groupAreaFlag){
        return ResultVOUtil.success();
    }


    @ApiOperation(value="暴雨组指标接口", notes="startTime：开始时间，endTime：结束时间")
    @GetMapping("/group/target")
    public ResultVO groupTarget(@RequestParam("startTime") String startTime,
                                @RequestParam("endTime") String endTime){
        return ResultVOUtil.success(rainstormCourseService.analysisGroupIndex(startTime,endTime));
    }

    @ApiOperation(value="查询单过程动画", notes="courseArea：过程编号 ，startTime：开始时间，endTime：结束时间，这些数据都是之前的列表接口返回的")
    @ApiImplicitParams({
            @ApiImplicitParam(name="courseArea",value="过程编号",dataType="string", paramType = "query",example=""),
            @ApiImplicitParam(name="startTime",value="结束时间",dataType="string", paramType = "query",example="20190723"),
            @ApiImplicitParam(name="endTime",value="开始时间",dataType="String", paramType = "query",example = "20190712")})
    @GetMapping("/getCourseAnimation")
    public ResultVO getCourseAnimation(@RequestParam("courseArea") String courseArea,@RequestParam("startTime") String startTime,@RequestParam("endTime") String endTime){
        return rainstormInfoService.getCourseAnimation(courseArea,startTime,endTime);
    }

    /**
     * 根据开始时间结束时间类型分析数据
     * @param startTime
     * @param endTime
     * @param type
     * @return
     */
    @ApiOperation(value="根据时间查暴雨分析信息列表", notes="startTime：开始时间，endTime：结束时间，type:区分是什么方法的，暂时写1")
    @GetMapping("/analysisByTarget")
    public ResultVO analysisByTarget(@RequestParam("startTime") String startTime,
                                   @RequestParam("endTime") String endTime,
                                     @RequestParam("type") Integer type){
        return rainstormInfoService.analysisByTarget(startTime,endTime,type);
    }

    /**
     * 筛选出暴雨事件
     * @param startYear
     * @param endYear
     * @return
     */
    @ApiOperation(value="将日降雨数据导入暴雨表时间维度是年", notes="筛选的是大于50mm小于2000mm的数据，startYear:2001  endYear:2012")
    @GetMapping("/initRainstormInfoToEs")
    public ResultVO initRainstormInfoToEs(@RequestParam("startYear") String startYear, @RequestParam("endYear") String endYear){
        rainstormInfoService.initRainstormInfoToEs(startYear,endYear);
        return ResultVOUtil.success();
    }

    /**
     * 筛选出暴雨事件
     * @param startTime
     * @param endTime
     * @return
     */
    @ApiOperation(value="将日降雨数据导入暴雨表", notes="筛选的是大于50mm小于2000mm的数据，startTime:20010101  endTime:20120101")
    @GetMapping("/initRainstormInfoToEsByTime")
    public ResultVO initRainstormInfoToEsByTime(@RequestParam("startTime") String startTime, @RequestParam("endTime") String endTime){
        rainstormInfoService.saveStorms(startTime,endTime,RectField.getBeanFields(StationCache.getStations(),"stationNo"));
        return ResultVOUtil.success();
    }

    /**
     * 保存单站点暴雨事件，到时候一年一年的跑
     * @param startYear
     * @param endYear
     * @return
     */
    @ApiOperation(value="保存单站点暴雨事件", notes="startYear:20111  endYear:2012")
    @GetMapping("/transSingleStrom")
    public ResultVO transSingleStrom(@RequestParam("startYear") Integer startYear,
                                     @RequestParam("endYear") Integer endYear){
//        rainstormInfoService.transSingleStrom(startTime,endTime);
        for (int i = startYear; i <= endYear; i++) {
            rainstormInfoService.transSingleStrom(i + "0101", i + "1231");
        }
        return ResultVOUtil.success();
    }



    /**
     * 保存区域暴雨事件，到时候一年一年的跑
     * @param startYear
     * @param endYear
     * @return
     */
    @ApiOperation(value="保存区域暴雨事件", notes="startYear:20111  endYear:2012")
    @GetMapping("/transAreaStrom")
    public ResultVO transAreaStrom(@RequestParam("startYear") Integer startYear,
                                   @RequestParam("endYear") Integer endYear){
        EsCondition esCondition = new EsCondition();
        esCondition.size(10000);
        List<Station> stations = stationInfoRepository.selectList(esCondition);
        List<String> stationNoList = RectField.getBeanFields(stations, "stationNo");
        for (int i = startYear; i <= endYear; i++) {
            rainstomCountService.countMain(i + "0101", i + "1231",stationNoList);
        }
        return ResultVOUtil.success();
    }

    /**
     * 计算暴雨站点组方法测试方法
     * @param startYear
     * @param endYear
     * @return
     */
    @ApiOperation(value="计算全国暴雨组数据入es", notes="startYear:20111  endYear:2012")
    @GetMapping("/nation/group/rainstom")
    public String rainstomNationGroup(@RequestParam("startYear") Integer startYear,
                                      @RequestParam("endYear") Integer endYear){
        EsCondition esCondition = new EsCondition();
        esCondition.size(10000);
        List<Station> stations = stationInfoRepository.selectList(esCondition);
        List<String> stationNoList = RectField.getBeanFields(stations, "stationNo");
        for (int i = startYear; i <= endYear; i++) {
            rainstormCourseService.countRainStormGroup(i + "0101", i + "1231",stationNoList);
        }
        return "sucess";
    }

    /**
     * 计算全国范围内的区域性暴雨过程
     * @param startYear
     * @param endYear
     * @return
     */
    @ApiOperation(value="计算全国暴雨组过程", notes="startYear:2011  endYear:2012")
    @GetMapping("/nation/group/area/rainstom")
    public String rainstomNationAreaGroup(@RequestParam("startYear") Integer startYear,
                                          @RequestParam("endYear") Integer endYear){

        for (int i = startYear; i <= endYear; i++) {
            rainstormCourseService.countRainStormAreaGroup(i + "0101", i + "1231");
        }
        return "sucess";
    }



    /**
     * 初始化中国持续性暴雨特征分析单站过程
     * @return
     */
    @ApiOperation(value="初始化中国持续性暴雨特征分析单站过程", notes="startYear:2000  endYear:2019")
    @GetMapping("/course/feature")
    public ResultVO initCourseFeature(@RequestParam("startYear") Integer startYear,
                                      @RequestParam("endYear") Integer endYear){
        for (int i = startYear; i <= endYear; i++) {
            rainstormFeatureService.transSingleStrom(i+"", i+"" );
        }
        log.info("初始化中国持续性暴雨特征分析单站过程完成");
        return ResultVOUtil.success();
    }

    /**
     * 初始化中国持续性暴雨特征分析区域过程
     * @return
     */
    @ApiOperation(value="初始化中国持续性暴雨特征分析区域过程", notes="startYear:2000  endYear:2019")
    @GetMapping("/featureAreaStrom")
    public ResultVO featureAreaStrom(@RequestParam("startYear") Integer startYear,
                                     @RequestParam("endYear") Integer endYear){
        EsCondition esCondition = new EsCondition();
        esCondition.size(10000);
        List<Station> stations = stationInfoRepository.selectList(esCondition);
        List<String> stationNoList = RectField.getBeanFields(stations, "stationNo");
        for (int i = startYear; i <= endYear; i++) {
            rainstormFeatureService.featureAreaStrom(i + "0101", i + "1231", stationNoList);
        }
        return ResultVOUtil.success();
    }
}
